package com.example.natatorium_management.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.natatorium_management.entity.MemberShip;
import com.example.natatorium_management.mapper.MemberShipMapper;
import com.example.natatorium_management.result.restful.R;
import com.example.natatorium_management.result.restful.ResponseEunm;
import com.example.natatorium_management.service.MemberShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
@Service
public class MemberShipServiceImpl extends ServiceImpl<MemberShipMapper, MemberShip> implements MemberShipService {
    @Autowired
    MemberShipMapper memberShipMapper;
    @Override
    public void addBatchMemberShip() {}
//        Random random = new Random();
//        List<MemberShip> list = new ArrayList<>();
//        for (int i = 0; i < 100; i++) {
//            list.add(new MemberShip(null,"null",new Date(),new Date(),i+10000000L,null,100L));
//        }
//        saveBatch(list);
//    }
//===========================================================================================================================


    /**
     * 新增会员
     * @param memberShip 1
     * @return 1
     */
    @Override
    public R<MemberShip> addMemberShip(MemberShip memberShip) {
        memberShip.setMemberShipCard(count()+10000001);
        memberShip.setStartTime(new Date());
        if(memberShipMapper.addMemberShip(memberShip) != 1){
            return new R<>(ResponseEunm.ADD_FAIL,null);
        }
        return new R<>(ResponseEunm.SUCCESS,null);
    }

    /**
     * 分页查询所有
     * @return d
     */
    @Override
    public R<IPage<MemberShip>> queryPage(int current,int size) {
        Page<MemberShip> page = Page.of(current, size);
        return new R<>(ResponseEunm.SUCCESS,memberShipMapper.queryPage(page));
    }

    /**
     * 更新会员
     * @param memberShip 1
     * @return 1
     */
    @Override
    public R<MemberShip> updateMemberShip(MemberShip memberShip){
        if (memberShipMapper.updateMemberShip(memberShip)==1){
            return new R<>(ResponseEunm.SUCCESS,null);
        }
        return new R<>(ResponseEunm.UPDATE_FAIL,null);
    }

    /**
     * 根据id查找
     * @param memberShipId 1
     * @return 1
     */
    @Override
    public R<MemberShip> findMemberShipById(int memberShipId) {
        return new R<>(ResponseEunm.SUCCESS,memberShipMapper.findMemberShipById(memberShipId));
    }

    /**
     * 搜索会员资格
     * @param memberShip q
     * @return q
     */
    @Override
    public R<IPage<MemberShip>> findMemberShip(MemberShip memberShip,int current,int size) {

        if (memberShip.getCustomerName()==null && memberShip.getPhone()==null){
        return new R<>(ResponseEunm.SUCCESS,memberShipMapper.findMemberShipByCard(Page.of(current,size),memberShip.getMemberShipCard()));
        }
        if (memberShip.getMemberShipCard()==null && memberShip.getCustomerName()==null){
            return new R<>(ResponseEunm.SUCCESS,memberShipMapper.findMemberShipByPhone(Page.of(current,size),memberShip.getPhone()));
        }
        if (memberShip.getMemberShipCard()==null && memberShip.getPhone()==null){
            return  new R<>(ResponseEunm.SUCCESS,memberShipMapper.findMemberShipByName(Page.of(current,size),memberShip.getCustomerName()));
        }
        if (memberShip.getMemberShipCard()==null){
            return new R<>(ResponseEunm.SUCCESS,memberShipMapper.findMemberShipByNameAndPhone(Page.of(current,size),memberShip));
        }
        if (memberShip.getCustomerName()==null){
            return  new R<>(ResponseEunm.SUCCESS,memberShipMapper.findMemberShipByCardAndPhone(Page.of(current,size),memberShip));
        }
        if (memberShip.getPhone()==null) {
            return new R<>(ResponseEunm.SUCCESS, memberShipMapper.findMemberShipByNameAndCard(Page.of(current,size),memberShip));
        }
        return new R<>(ResponseEunm.SUCCESS,memberShipMapper.findMemberShipByCardAndPhoneAndName(Page.of(current,size),memberShip));
    }

    /**
     * 删除
     * @param memberShipId 1
     * @return 1
     */
    @Override
    public R<MemberShip> deleteMemberShipById(int memberShipId) {
        if(memberShipMapper.deleteMemberShipById(memberShipId)==1){
            return new R<>(ResponseEunm.SUCCESS,null);
        }
        return new R<>(ResponseEunm.DELETE_FAIL,null);
    }
}
